package com.zzyweb.managementsystem.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.zzyweb.managementsystem.pojo.UserInfo;
import com.zzyweb.managementsystem.pojo.vo.UserInfoVO;
import com.zzyweb.managementsystem.service.UserService;
import com.zzyweb.managementsystem.token.MyJWTUtil;
import com.zzyweb.managementsystem.util.RedisUtil;
import com.zzyweb.managementsystem.util.ResponseObject;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhiyi
 * @date 9/22/25
 **/
@RestController
@CrossOrigin
@RequestMapping("/user")
public class LogInController {
    @Autowired
    private RedisUtil redisUtil;

    @Resource
    private UserService userService;
    @GetMapping("/createCaptch")
    public ResponseObject createCaptch(String captchaId){
        System.out.println("test");
        Map<String,String> map = new HashMap<>();
        //先判断在redis中有没有codeId的数据，如果没有就是第一次登陆，如果有就不是
        if(redisUtil.get("captchaId") == null || redisUtil.get("captchaId").trim().isEmpty()) {
            //第一次登陆 创建codeid 用于记录verify code 对应的用户
            //这里查不到，但是如果传了codeId 证明codeId在redis中已经过期了，需要进行更新
            captchaId= IdUtil.getSnowflakeNextIdStr();
            System.out.println(captchaId);
        }
        //设置验证码图片长、宽、验证码字符数、干扰线宽度
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 30, 4, 10);
        //设置字体样式和大小
        lineCaptcha.setFont(new Font("仿宋", Font.BOLD, 30));
        //获取验证码
        String code = lineCaptcha.getCode();
        redisUtil.set(captchaId,code,120L);
        System.out.println(code);
        //转为Base64字符串
        String imageBase64 = lineCaptcha.getImageBase64Data();

        //在有了codeId 和图案以后，一起返回给前端
        map.put("captchaId", captchaId);
        map.put("captchaUrl", imageBase64);
        return ResponseObject.ok(map);
    }

    @PostMapping("/logIn")
    public ResponseObject logIn(@RequestBody UserInfoVO userInfoVO, @RequestHeader(value = "Authorization",required = false) String authHeader) {
        System.out.println("login");
        Map<String, Object> res = new HashMap<>();
        //1，先判断验证码，验证码存在于redis中 若不存在或者报错，已经抛出异常
        /*String code = redisUtil.get(userInfoVO.getCodeId());
        if (code == null || code.trim().isEmpty()) {
            return ResponseObject.error().msg("验证码失效 重新登陆");
        }
        if (!code.equalsIgnoreCase(userInfoVO.getVerifyCode())) {

        }*/
        System.out.println(userInfoVO);
        userService.verifyCode(userInfoVO.getCaptchaId(), userInfoVO.getVerifyCode());
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userInfoVO, userInfo);
        userService.logIn(userInfo);
        UserInfo user = userService.getUserByUserName(userInfoVO.getUsername());
        String token = null;
        if (authHeader == null || authHeader.trim().isEmpty()) {
            token = MyJWTUtil.createToken(userInfo);
        } else {
            token = authHeader.substring(7);
        }
        res.put("username", user.getUsername());
        res.put("userId", user.getId());
        res.put("imgPath", user.getImgPath());
        res.put("accessToken", token);
        return ResponseObject.ok(res).msg("登陆成功");
    }

    @GetMapping("/register")
    public ResponseObject registration(@RequestBody UserInfo userInfo) {
        userService.register(userInfo);
        return ResponseObject.ok();
    }
}
